package com.cloud.quickcore.admin.service;

import com.cloud.quickcommon.biz.BaseBizService;
import com.cloud.quickcore.admin.dao.SysPermissionRepository;
import com.cloud.quickcore.admin.dto.PermissionDto;
import com.cloud.quickcore.admin.entity.SysPermission;
import com.cloud.quickcore.admin.entity.SysRole;
import com.cloud.quickcore.admin.vo.PermissionTree;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 菜单资源--Service
 *
 * @author NiJo_H
 * @version 1.0
 * @date 2019/8/13 21:17
 */
@Service
public class SysPermissionService implements BaseBizService<PermissionDto, SysPermission, Long> {
    @Autowired
    private SysPermissionRepository permissionRepository;

    @Override
    public SysPermission save(PermissionDto dto) {
        SysPermission sysPermission = dto.convertEntity();
        return permissionRepository.save(sysPermission);
    }

    @Override
    public SysPermission update(PermissionDto dto) {
        return null;
    }

    @Override
    public void romve(Long aLong) {

    }

    @Override
    public Optional<SysPermission> findById(Long aLong) {
        return Optional.empty();
    }

    @Override
    public List<SysPermission> findAll() {
        return permissionRepository.findAll();
    }


    /**
     * 通过角色信息查询对应菜单信息
     */
    public List<PermissionTree> findMenuTreeByRoles(Set<SysRole> roles) {
        if (CollectionUtils.isEmpty(roles)) {
            return Lists.newArrayList();
        }
        /**超级管理员查询所有菜单*/
        if (roles.stream().map(SysRole::getRoleCode).collect(Collectors.toSet()).contains("superadmin")) {
            return this.createTree(this.findAll().stream().filter(x -> x.getResourceType() == 1).collect(Collectors.toList()));
        }

        /**非超级管理员*/
        Set<SysPermission> permissions= Sets.newHashSet();
        for(SysRole role:roles){
            permissions.addAll(role.getPermissions());
        }
        return this.createTree(permissions.stream().filter(x->x.getResourceType()==1).collect(Collectors.toList()));
    }

    /**
     * 创建权限树
     */
    private List<PermissionTree> createTree(List<SysPermission> rootPermissions) {
        // 先找到所有一级菜单
        List<PermissionTree> permissionTrees = rootPermissions.stream()
                .filter(permission -> Objects.isNull(permission.getParentId()) || permission.getParentId() == 0)
                .map(PermissionTree::new).sorted().collect(Collectors.toList());

        // 过滤掉一级菜单的菜单列表
        List<SysPermission> permissions = rootPermissions.stream()
                .filter(permission -> Objects.nonNull(permission.getParentId())).collect(Collectors.toList());

        permissionTrees.forEach(x -> x.setChildren(createChildren(x.getId(), permissions)));
        return permissionTrees;
    }

    /**
     * 递归创建子权限树
     */
    private List<PermissionTree> createChildren(Long id, List<SysPermission> permissions) {
        List<PermissionTree> children = permissions.stream().filter(permission -> id.equals(permission.getParentId()))
                .map(PermissionTree::new).sorted().collect(Collectors.toList());
        if (children.isEmpty()) {
            return null;
        } else {
            children.forEach(x -> x.setChildren(createChildren(x.getId(), permissions)));
        }
        return children;
    }

}
